package com.example.springbootdemo.cloneable.deep;

import com.example.springbootdemo.cloneable.shallow.ShallowSubject;

/**
 * @author cici
 */
public class DeepStudent implements Cloneable {

    /**
     * 引用类型
     */
    private DeepSubject subject;

    /**
     * 基本数据类型
     */
    private String name;
    private int age;

    public DeepSubject getSubject() {
        return subject;
    }

    public void setSubject(DeepSubject subject) {
        this.subject = subject;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    /**
     *  重写clone()方法
     * @return
     */
    @Override
    public Object clone() {
        //深拷贝
        try {
            // 直接调用父类的clone()方法
            DeepStudent student = (DeepStudent) super.clone();
            student.subject = (DeepSubject) subject.clone();
            return student;
        } catch (CloneNotSupportedException e) {
            return null;
        }

    }

    @Override
    public String toString() {
        return "[Student: " + this.hashCode() + ",subject:" + subject + ",name:" + name + ",age:" + age + "]";
    }

}
